namespace DotNetNuke.Entities.Users
{
    using DotNetNuke.Entities.Portals;
    using System;
    using System.Web;

    public class UserTime
    {
        public DateTime ConvertToServerTime(DateTime dt, double ClientTimeZone)
        {
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            return dt.AddMinutes(this.FromServerToClientFactor(ClientTimeZone, (double) _portalSettings.TimeZoneOffset));
        }

        public DateTime ConvertToUserTime(DateTime dt, double ClientTimeZone)
        {
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            return dt.AddMinutes(this.FromClientToServerFactor(ClientTimeZone, (double) _portalSettings.TimeZoneOffset));
        }

        private double FromClientToServerFactor(double Client, double Server)
        {
            return (Client - Server);
        }

        private double FromServerToClientFactor(double Client, double Server)
        {
            return (Server - Client);
        }

        public double ClientToServerTimeZoneFactor
        {
            get
            {
                PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
                UserInfo objUserInfo = UserController.GetCurrentUserInfo();
                return this.FromClientToServerFactor((double) objUserInfo.Profile.TimeZone, (double) _portalSettings.TimeZoneOffset);
            }
        }

        public DateTime CurrentUserTime
        {
            get
            {
                if (!HttpContext.Current.Request.IsAuthenticated)
                {
                    return DateTime.Now;
                }
                return DateTime.Now.AddMinutes(this.ClientToServerTimeZoneFactor);
            }
        }

        public double ServerToClientTimeZoneFactor
        {
            get
            {
                UserInfo objUserInfo = UserController.GetCurrentUserInfo();
                PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
                return this.FromServerToClientFactor((double) objUserInfo.Profile.TimeZone, (double) _portalSettings.TimeZoneOffset);
            }
        }
    }
}

